package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.CustomBean;
import com.project.mapper.ICustomMapper;
import com.project.service.ICustomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CustomServiceImp implements ICustomService {
    @Autowired
    private ICustomMapper mapper;

    @Override
    public IPage<CustomBean> cutByItem(Integer pageNO, String name, String scope) {
        QueryWrapper qw = new QueryWrapper<>();
        if(name!=null&&!name.equals("")) {
            qw.like("c_name", name);
        }
        if(scope!=null&&!scope.equals("")) {
            qw.like("c_scope", scope);
        }
        return mapper.selectPage(new Page<>(pageNO,5),qw);
    }

    @Override
    public CustomBean findById(Integer id) {
        return mapper.selectById(id);
    }

    @Override
    public void update(Integer id, String director, String phone, String scope) {
        CustomBean customBean = mapper.selectById(id);
        customBean.setPhone(phone);
        customBean.setDirector(director);
        customBean.setScope(scope);
        mapper.updateById(customBean);
    }

    @Override
    public CustomBean findByName(String name) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("c_name",name);
        return mapper.selectOne(qw);
    }

    @Override
    public void add(CustomBean customBean) {
        mapper.insert(customBean);
    }

    @Override
    public List<CustomBean> findAll() {
        return mapper.selectList(null);
    }
}
